<template>
  <div>
    <el-table :data="roles" style="width: 100%">
      <el-table-column prop="role" label="角色" />
      <el-table-column prop="permissions" label="权限">
        <template #default="scope">
          <el-tag v-for="perm in scope.row.permissions" :key="perm">{{ perm }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="mini" @click="editRole(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" title="编辑权限">
      <el-form :model="form">
        <el-form-item label="角色">
          <el-input v-model="form.role" disabled />
        </el-form-item>
        <el-form-item label="权限">
          <el-checkbox-group v-model="form.permissions">
            <el-checkbox label="用户管理" />
            <el-checkbox label="数据统计" />
            <el-checkbox label="系统设置" />
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveRole">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from 'vue'
const roles = ref([
  { role: 'admin', permissions: ['用户管理', '数据统计', '系统设置'] },
  { role: 'user', permissions: ['数据统计'] }
])
const dialogVisible = ref(false)
const form = ref({ role: '', permissions: [] })
function editRole(row) {
  form.value = { ...row }
  dialogVisible.value = true
}
function saveRole() {
  const idx = roles.value.findIndex(r => r.role === form.value.role)
  roles.value[idx] = { ...form.value }
  dialogVisible.value = false
}
</script>
